﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zuJianClient.PacketDto
{
    /// <summary>
    /// 白名单信息
    /// </summary>
    public class Whitelist
    {
        /// <summary>
        /// 项目编号
        /// </summary>
        /// <remarks>字节数：4，格式：HEX，说明：低位在前</remarks>
        public uint ProjectNumber { get; set; }

        /// <summary>
        /// 项目名称
        /// </summary>
        /// <remarks>字节数：100，格式：UTF-8，说明：用于机器显示用</remarks>
        public string ProjectName { get; set; } = string.Empty;

        /// <summary>
        /// 数据记录列表
        /// </summary>
        public List<DataRecord> Records { get; set; } = new List<DataRecord>();

        public static class PacketParser
        {
            /// <summary>
            /// 从十六进制字符串解析白名单信息
            /// </summary>
            /// <param name="hexString"></param>
            /// <returns></returns>
            /// <exception cref="ArgumentException"></exception>
            public static Whitelist ParseWhitelistFromHex(string hexString)
            {
                if (string.IsNullOrEmpty(hexString) || hexString.Length % 2 != 0)
                    throw new ArgumentException("Invalid hex string.");

                var bytes = StringToByteArray(hexString);
                int currentIndex = 0;

                // 读取指定字节数并递增索引的辅助函数
                byte[] ReadBytes(int count)
                {
                    byte[] result = new byte[count]; // 按指定长度创建新字节数组
                    Array.Copy(bytes, currentIndex, result, 0, count); // 复制指定长度的字节
                    currentIndex += count;
                    return result;
                }

                Whitelist whitelist = new Whitelist();

                // 解析项目编号（4 个字节）
                whitelist.ProjectNumber = (uint)HightLowConver(ReadBytes(4));

                // 解析项目名称（100 字节）
                whitelist.ProjectName = Encoding.UTF8.GetString(ReadBytes(100)).TrimEnd('\0');

                // 解析数据记录
                whitelist.Records = new List<DataRecord>();
                while (currentIndex + 36 <= bytes.Length) // 最小记录长度为 36 字节
                {
                    DataRecord record = new DataRecord();

                    // 解析工作者编号（4 个字节）
                    record.WorkerNumber = BitConverter.ToUInt32(ReadBytes(4));

                    // 解析工作者名称（10 字节）
                    record.WorkerName = Encoding.UTF8.GetString(ReadBytes(10)).TrimEnd('\0');

                    // 解析 ID 号（18 个字节）
                    record.IDNumber = Encoding.ASCII.GetString(ReadBytes(18)).TrimEnd('\0');

                    // 解析照片长度（4 字节）
                    record.PhotoLength = BitConverter.ToUInt32(ReadBytes(9), 0);

                    whitelist.Records.Add(record);
                }

                return whitelist;
            }


        }
    }

    public class DataRecord
    {
        /// <summary>
        /// 工人编号
        /// </summary>
        /// <remarks>字节数：4，格式：HEX</remarks>
        public uint WorkerNumber { get; set; }

        /// <summary>
        /// 工人姓名
        /// </summary>
        /// <remarks>字节数：10，格式：UTF-8</remarks>
        public string WorkerName { get; set; } = string.Empty;

        /// <summary>
        /// 身份证号码
        /// </summary>
        /// <remarks>字节数：18，格式：ASCII</remarks>
        public string IDNumber { get; set; } = string.Empty;

        /// <summary>
        /// 采集照片长度
        /// </summary>
        /// <remarks>字节数：4，格式：Hex</remarks>
        public uint PhotoLength { get; set; }
    }

}
